<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>我的拼团订单</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css" />
    <link rel="stylesheet" type="text/css" href="../css/layer.css" />
    <link rel="stylesheet" type="text/css" href="../css/mdw.css" />
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../script/swiper.min.js"></script>
    <script type="text/javascript" src="../script/mobile.js"></script>
    <script type="text/javascript" src="../script/layer.js"></script>
    <script type="text/javascript" src="../script/mdw.js"></script>
    <script type="text/javascript" src="../script/jumpPage.js"></script>

</head>
<body>
    <header class="header">
        <div class="backPrevPage" onclick="historyBack()"></div>
        <div class="pageTitle">我的拼团订单</div>
    </header>
    <section class="HTML_BOX">
        <ul class="orderClass displayFlex">
            <li class="on f_c">全部<i></i></li>
            <li class="f_c">待付款<i></i></li>
            <li class="f_c">待发货<i></i></li>
            <li class="f_c">待收货<i></i></li>
            <li class="f_c">待评价<i></i></li>
        </ul>
        <div class="bg-eee" style="border-bottom:.2rem solid #EEE ; "></div>
        <ul class="orderList bg-eee">
            
        </ul>
        
    </section>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
     var page =1;
    var type =0;
    var param = {};
    var ifpage = true;
    apiready = function(){
        $api.fixStatusBar($api.dom('header'));
        api.setStatusBarStyle({
            color: '#fff',//设置APP状态栏背景色
            style: 'dark'
        });
        api.addEventListener({
            name: 'scrolltobottom',
            extra: {
                 threshold: 10
            }
         }, function(ret, err) {
             //页码+1，继续请求数据
             
             if(ifpage){
                page++;
                orderList();
             }
        });
        param = api.pageParam || {};
        $(".orderClass li").removeClass("on");
        type = param.type || 0;
        $(".orderClass li").eq(type).addClass("on");
        ifCnAndEn();
        orderList();
    };
    function orderList(){
        var p = 
        {
            token:token,
            type:type,
            page:page,
            buy_type:"spell"
        }
        pop.loading();
        getAjax(Interface.member_orders,p,function(res){
            pop.close();
            console.log(JSON.stringify(res));
            var list = res.data;
            if(page==1){
                $(".orderList").empty();
            }
            if(list){
                $(".pageHide").addClass("dn");ifpage=true;
                $.each(list,function(i,item){
                    var g = item.group;
                    var html='<li onclick="goOrder('+item.id+')" class="bg-fff plr">';
                            html+='<div class="displayFlex orderTitle">';
                            var btns = orderBtn(item.order_type,item);
                            if(cnen==1){
                               html+='<span>订单编号：'+item.order_number+'</span>'; 
                            }else{
                               html+='<span>Order No：'+item.order_number+'</span>';
                               item.order_type = item.enorder_type;
                            }
                            if(item.order_type != '待付款' && item.group.state!=2){
                                if(item.group.state==1){
                                    item.order_type="拼团中，还差"+(Number(g.total_num)-g.pintuan_num)+"人";
                                    if(cnen==2){
                                        item.order_type="Group buying needs "+(Number(g.total_num)-g.pintuan_num)+" people";
                                    }
                                }else if(item.group.state==3){
                                    item.order_type="拼团失败，已退款";
                                    if(cnen==2){
                                        item.order_type="Group purchase failure, refund";
                                    }
                                }
                            }
                            html+='<span class="orderState">'+item.order_type+'</span>';
                            html+='</div>';
                            if(item.product){
                                $.each(item.product,function(j,jtem){
                                var name = '';
                                if(cnen==1){
                                    name = jtem.title;
                                }else{
                                    name = jtem.egname;
                                }
                                html+='<div class="orderInfo">';
                                html+='<div onclick="goWare(\''+jtem.id+'\')" class="wareInfo">';
                                    html+='<div class="wareImage"><img src="'+getImg(jtem.icon)+'" /></div>';
                                    html+='<div class="wareData">';
                                        html+='<div class="wareName">'+name+'</div>';
                                        //html+='<div class="wareGuiGe">2kg</div>';
                                        html+='<div class="warePrice">';
                                        if(jtem.old_price == ""){
                                            tml+='<div><span class="prcie1">$'+jtem.one_price+'</span></div>';
                                            html+='<span>x'+jtem.num+'</span>';
                                        }else{
                                            html+='<div><span class="prcie1">$'+jtem.one_price+'</span><del class="prcie2">$'+jtem.old_price+'</del></div>';
                                            html+='<span>x'+jtem.num+'</span>';
                                        }
                                        html+='</div>';
                                    html+='</div>';
                                html+='</div>';

                            })
                            }
                            
                            html+='<div class="orderPrcie">';
                                html+='<span class="tolPrice">$'+item.pay_price+'</span>';
                            if(cnen==1){
                                html+='<span class="count">共'+item.total_num+'件</span>';
                            }else{
                                html+='<span class="count"> total of '+item.total_num+' pieces</span>';
                            }   
                            html+='</div>';
                            html+='<div class="orderTime">';
                            if(cnen==1){
                                html+='<div class="time f_c">下单时间：'+timestampToTime(item.create_time)+'</div>';
                            }else{
                                html+='<div class="time f_c">Create Time：'+timestampToTime(item.create_time)+'</div>';
                            }
                                html+='<div class="btns">';
                                    html += btns;
                                html+='</div>';
                            html+='</div>';
                       html+=' </li>';
                    $(".orderList").append(html);
                })
                if(list.length<10){
                    $(".getPage").hide();
                }
            }else{
                 ifpage=false;
                 if(page==1){$(".pageHide").removeClass("dn");}
            }
        },function(res,err){})
    }
    $(".getPage").click(function(){
        page++;
        orderList();
    })
    $(".go_ware").click(function(event){
        event.stopPropagation();
        openWin('ware','ware.html');
    })
    $(".orderClass li").click(function(){
        $(".orderClass li").removeClass("on");
        $(this).addClass("on");
        type = $(this).index();
        orderList();
    })
    //去付款
    function go_pay(id,price){
        event.stopPropagation();
        var p = {
            order_number:id,
            sum_price:price,
            buy_type:"spell",
        }
        openWin('ware_payOrder','ware_payOrder.html',p);
    }
    //去评价
    function go_eva(id){
        event.stopPropagation();
        openWin('order_chooseEva','order_chooseEva.html',{order_id:id});
    }
    //去详情
    function goOrder(id){
        console.log(id);
        openWin('groupBuy_orderDetails','groupBuy_orderDetails.html',{id:id});
    }
    //去邀请
    function go_yaoqing(id){
        event.stopPropagation();
        openWin('groupBuy_details','groupBuy_details.html',{id:id});
    }
    //取消订单
    function quxiao_order(id){
        event.stopPropagation();
        getAjax(Interface.quxiao_order,{token:token,order_id:id},function(res){
            pop.notice(res.msg,1,function(){
                api.sendEvent({
                    name: 'duihuanyhq',
                    extra: {}
                });
                page=1;
                orderList();
            });
        },function(res,err){})
    }
    function buy_again(id){
        event.stopPropagation();
        pop.loading();
        getAjax(Interface.buy_again,{token:token,order_id:id},function(res){
            pop.close();
            shoppingCart();
        },function(res,err){})
    }
    function orderBtn(type,item){
        var html ='';
        console.log(item.group.state);
        if(type != '待付款'&&item.group.state && item.group.state!=2){
            if(item.group.state==1){
                if(cnen==1){
                    // html+='<div nclick="quxiao_order('+item.id+');" class="btn2 f_c">取消订单</div>';
                    html+='<div onclick="go_yaoqing('+item.group_id+');" class="btn1 pingjia f_c">邀请好友</div>';
                }else{
                    // html+='<div nclick="quxiao_order('+item.id+');" class="btn2 go_ware f_c">Repurchase</div>';
                    html+='<div onclick="go_yaoqing('+item.group_id+');" class="btn1 pingjia f_c">Invite friends</div>';
                }
            }else if(item.group.state==3){
                
            }
        }else{
            if(type == '待付款'){
                if(cnen==1){
                    html+='<div onclick="go_pay('+item.order_number+','+item.pay_price+');" class="btn1 pingjia f_c">去付款</div>';
                }else{
                    html+='<div onclick="go_pay('+item.order_number+','+item.pay_price+');" class="btn1 pingjia f_c">Pay</div>';
                }
            }else if(type == '待发货'){
                if(cnen==1){
                   html+='<div class="btn1 pingjia f_c">确认收货</div>';
                }else{
                   html+='<div class="btn1 pingjia f_c">Confirm receipt</div>';
                }
            }else if(type == '待评价'){
                if(cnen==1){
                // html+='<div onclick="buy_again('+item.id+');" class="btn2 f_c">再次购买</div>';
                html+='<div onclick="go_eva('+item.id+');" class="btn1 pingjia f_c">评价</div>';
            }else{
                // html+='<div onclick="buy_again('+item.id+');" class="btn2 go_ware f_c">Repurchase</div>';
                html+='<div onclick="go_eva('+item.id+');" class="btn1 pingjia f_c">Review</div>';
            }
                
            }else if(type == '订单已取消'){
                html+='<div class="btn2 go_ware f_c">Repurchase</div>';
            }
        }
        
        return html;
    }


    
</script>
<script type="text/javascript">
    function ifCnAndEn(){
        if(cnen==2){
            $(".pageTitle").eq(0).html('Group Buy Orders');
            $(".orderClass li").eq(0).html('All<i></i>');
            $(".orderClass li").eq(1).html('Unpaid<i></i>');
            $(".orderClass li").eq(2).html('Processing<i></i>');
            $(".orderClass li").eq(3).html('Shipped<i></i>');
            $(".orderClass li").eq(4).html('For Review<i></i>');
            $(".getPage").html('Click to load more...');
            $(".pageHide").html('Not data');
        }
    }
</script>
</html>